package com.bytedance.frameworks.baselib.network.http.parser;

import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.Locale;

public class MimeType implements Externalizable {
    private static final String TSPECIALS = "()<>@,;:/[]?=\\\"";
    private com.bytedance.frameworks.baselib.network.http.parser.b parameters;
    private String primaryType;
    private String subType;

    public MimeType() {
        this.primaryType = "application";
        this.subType = "*";
        this.parameters = new b();
    }

    public MimeType(String arg1) throws MimeTypeParseException {
        this.parse(arg1);
    }

    public MimeType(String arg3, String arg4) throws MimeTypeParseException {
        if (this.isValidToken(arg3)) {
            this.primaryType = arg3.toLowerCase(Locale.ENGLISH);
            if (this.isValidToken(arg4)) {
                this.subType = arg4.toLowerCase(Locale.ENGLISH);
                this.parameters = new b();
                return;
            }

            throw new MimeTypeParseException("Sub type is invalid.");
        }

        throw new MimeTypeParseException("Primary type is invalid.");
    }

    private static boolean isTokenChar(char arg1) {
        boolean v0 = arg1 <= 32 || arg1 >= 127 || "()<>@,;:/[]?=\\\"".indexOf(arg1) >= 0 ? false : true;
        return v0;
    }

    public String getBaseType() {
        return this.primaryType + "/" + this.subType;
    }

    public String getParameter(String arg4) {
        return (String) this.parameters.a.get(arg4.trim().toLowerCase(Locale.ENGLISH));
    }

    public b getParameters() {
        return this.parameters;
    }

    public String getPrimaryType() {
        return this.primaryType;
    }

    public void setPrimaryType(String arg3) throws MimeTypeParseException {
        if (!this.isValidToken(this.primaryType)) {
            throw new MimeTypeParseException("Primary type is invalid.");
        }

        this.primaryType = arg3.toLowerCase(Locale.ENGLISH);
    }

    public String getSubType() {
        return this.subType;
    }

    public void setSubType(String arg3) throws MimeTypeParseException {
        if (!this.isValidToken(this.subType)) {
            throw new MimeTypeParseException("Sub type is invalid.");
        }

        this.subType = arg3.toLowerCase(Locale.ENGLISH);
    }

    private boolean isValidToken(String arg5) {
        boolean v0 = false;
        int v2 = arg5.length();
        if (v2 > 0) {
            int v1 = 0;
            while (true) {
                if (v1 >= v2) {
                    break;
                } else if (MimeType.isTokenChar(arg5.charAt(v1))) {
                    ++v1;
                    continue;
                }

                return v0;
            }

            v0 = true;
        }

        return v0;
    }

    public boolean match(MimeType arg3) {
        boolean v0;
        if (this.primaryType.equals(arg3.getPrimaryType())) {
            if (!this.subType.equals("*") && !arg3.getSubType().equals("*") && !this.subType.equals(arg3.getSubType())) {
                return false;
            }
            v0 = true;
        } else {
            v0 = false;
        }
        return v0;
    }

    public boolean match(String arg2) throws MimeTypeParseException {
        return this.match(new MimeType(arg2));
    }

    private void parse(String arg5) throws MimeTypeParseException {
        int v0 = arg5.indexOf(47);
        int v1 = arg5.indexOf(59);
        if (v0 < 0 && v1 < 0) {
            throw new MimeTypeParseException("Unable to find a sub type.");
        }

        if (v0 < 0 && v1 >= 0) {
            throw new MimeTypeParseException("Unable to find a sub type.");
        }

        if (v0 >= 0 && v1 < 0) {
            this.primaryType = arg5.substring(0, v0).trim().toLowerCase(Locale.ENGLISH);
            this.subType = arg5.substring(v0 + 1).trim().toLowerCase(Locale.ENGLISH);
            this.parameters = new b();
        } else if (v0 < v1) {
            this.primaryType = arg5.substring(0, v0).trim().toLowerCase(Locale.ENGLISH);
            this.subType = arg5.substring(v0 + 1, v1).trim().toLowerCase(Locale.ENGLISH);
            this.parameters = new b(arg5.substring(v1));
        } else {
            throw new MimeTypeParseException("Unable to find a sub type.");
        }

        if (!this.isValidToken(this.primaryType)) {
            throw new MimeTypeParseException("Primary type is invalid.");
        }

        if (!this.isValidToken(this.subType)) {
            throw new MimeTypeParseException("Sub type is invalid.");
        }
        return;
    }

    public void readExternal(ObjectInput arg3) throws IOException, ClassNotFoundException {
        try {
            this.parse(arg3.readUTF());
            return;
        } catch (MimeTypeParseException v0) {
            throw new IOException(v0.toString());
        }
    }

    public void removeParameter(String arg4) {
        this.parameters.a.remove(arg4.trim().toLowerCase(Locale.ENGLISH));
    }

    public void setParameter(String arg4, String arg5) {
        this.parameters.a.put(arg4.trim().toLowerCase(Locale.ENGLISH), arg5);
    }

    public String toString() {
        return this.getBaseType() + this.parameters.toString();
    }

    public void writeExternal(ObjectOutput arg2) throws IOException {
        arg2.writeUTF(this.toString());
        arg2.flush();
    }
}

